﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.talent.dao.impl;

import java.util.*;

import com.hh.bss.base.*;
import com.hh.bss.common.ConstantDic;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.talent.dao.*;
import com.hh.bss.talent.entity.*;
import com.hh.bss.talent.service.*;
import com.hh.bss.talent.vo.query.*;
import com.hh.bss.util.*;





import static com.hh.bss.util.ObjectUtils.*;

import org.springframework.stereotype.Repository;

@Repository
public class CvAttachmentDaoImpl extends BaseHibernateDao<CvAttachment,java.lang.Integer>  implements CvAttachmentDao<CvAttachment,java.lang.Integer>{

	public Class getEntityClass() {
		return CvAttachment.class;
	}
	
	public Page findPage(CvAttachmentQuery query) {
        //XsqlBuilder syntax,please see http://code.google.com/p/rapid-xsqlbuilder
        // [column]为字符串拼接, {column}为使用占位符. [column]为使用字符串拼接,如username='[username]',偷懒时可以使用字符串拼接 
        // [column] 为PageRequest的属性
        
		
		    Class[] os={CvAttachmentQuery.class};
    		Class property= org.springframework.beans.BeanUtils.findPropertyType(query.getField_type(), os);
        if (property!=null){
        	 if ("java.lang.String".equals(property.getName())){
	    			
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(),query.getQuery());
	    			
	    		}
	    		else if ("java.lang.Integer".equals(property.getName())){
	    			
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseInteger(query.getQuery(), 0));
	    			
	    		}
	    		else if ("java.lang.Double".equals(property.getName())){
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseDouble(query.getQuery(), 0));
	    		}
	    		else if ("java.util.Date".equals(property.getName())){
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), DateHelper.toDate(query.getQuery(), DateHelper.DEFAULT_DATE_FORMAT));
	    		}
        }	 
        //生成sql2的原因是为了不喜欢使用xsqlbuilder的同学，请修改生成器模板，删除本段的生成
        /*
        StringBuilder sql2 = new StringBuilder("select t from CvAttachment t where 1=1 ");
          if(isNotEmpty(query.getId())) {
            sql2.append(" and  t.id = :id ");
        }
          if(isNotEmpty(query.getCvId())) {
            sql2.append(" and  t.cvId = :cvId ");
        }
        if(isNotEmpty(query.getName())) {
            sql2.append(" and  t.name like :name ");
            query.setName("%"+query.getName()+"%"); 

        }
        if(isNotEmpty(query.getFileName())) {
            sql2.append(" and  t.fileName like :fileName ");
            query.setFileName("%"+query.getFileName()+"%"); 

        }
        if(isNotEmpty(query.getFileType())) {
            sql2.append(" and  t.fileType like :fileType ");
            query.setFileType("%"+query.getFileType()+"%"); 

        }
          if(isNotEmpty(query.getFileSize())) {
            sql2.append(" and  t.fileSize = :fileSize ");
        }
        if(isNotEmpty(query.getCreator())) {
            sql2.append(" and  t.creator like :creator ");
            query.setCreator("%"+query.getCreator()+"%"); 

        }
        if(isNotEmpty(query.getCreateDtBegin())) {
            sql2.append(" and  t.createDt >= :createDtBegin ");
        }
        if(isNotEmpty(query.getCreateDtEnd())) {
            sql2.append(" and  t.createDt <= :createDtEnd ");
        }
        if(isNotEmpty(query.getSortColumns())) {
            sql2.append(" order by :sortColumns ");
        }	
        */
        
        StringBuffer sql = new StringBuffer();
		//单独给CC项目的相关职位使用的
		List values = new ArrayList();
		sql.append(" select * from ( ");
		sql.append(" select a.id,a.cv_id cvId,a.name,a.file_name fileName,a.file_type fileType,a.type,a.file_size fileSize,a.creator,a.create_dt createDt from cv_attachment a  where 1=1 ");
	    if(isNotEmpty(query.getCvId())) {
            sql.append(" and  a.cv_id = ? ");
            values.add(query.getCvId());
        }
	/*	sql.append(" union all ");
		sql.append(" select concat('cr',b.id) id,b.cv_id cvId,b.subject name,b.attachment fileName,'' fileType,'CR' type,0 fileSize,b.creator,b.create_dt createDt from contract_job_cv_jd b where 1=1 ");
		if(isNotEmpty(query.getCvId())) {
            sql.append(" and length(b.attachment)>0 and  b.cv_id = ? and  b.type = 2 ");
            values.add(query.getCvId());
        }
		sql.append(" union all ");
		sql.append(" select concat('jd',b.id) id,b.cv_id cvId,b.subject name,b.attachment2 fileName,'' fileType,'JD' type,0 fileSize,b.creator,b.create_dt createDt from contract_job_cv_jd b where  length(b.attachment2)>0 ");
		if(isNotEmpty(query.getCvId())) {
            sql.append(" and  b.cv_id = ? and b.type = 1");
            values.add(query.getCvId());
        }*/
		sql.append(" ) cvatt ");
		
		sql.append(" where 1=1 ");
		
		if(isNotEmpty(query.getType())){
			sql.append(" and cvatt.type = ? ");
			values.add(query.getType());
		}
		sql.append("  order by cvatt.createDt desc ");
		return super.getJdbcDao().createPage(sql.toString(),values.toArray(), new MapObjectRowMapper(), query.getPageSize(), query.getPageNumber());
		//return pageQuery(sql2.toString(),query);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	
	public void updateCvResume(CvAttachment cvAttachment) {
		

		@SuppressWarnings("unused")
		//List list = this.getHibernateTemplate().find(" from  Cv ");
		List   cvList = this.getHibernateTemplate().find(" from  Cv cv where cv.id = ? " ,cvAttachment.getCvId());
		if(cvList !=null && cvList.size() ==1){
			Cv cv = (Cv)cvList.get(0);
			if(cv.getResumeCreator() == null ){
				cv.setResumeCreator(cvAttachment.getCreator());
			}
			if(cv.getResumeDt() == null){
				cv.setResumeDt(cvAttachment.getCreateDt());
			}
			if(cv.getCvStatus() == null || "T".equals(cv.getCvStatus()) ){
				cv.setCvStatus("F");
			}
			
			String hql = " from CvAttachment c where c.cvId = ?  " ;
			List achmentList = this.getHibernateTemplate().find(hql, cvAttachment.getCvId());
			if(achmentList !=null && achmentList.size() == 0 ){
				if(cv.getResumeCreator() == null )
				 cv.setResumeInformer(cvAttachment.getCreator());
			}
			this.getHibernateTemplate().update(cv);
		}
	
		
		
	}

	
	public Integer findAttachmentCountByCvId(Integer cvId) {
		String hql = " from CvAttachment c where c.cvId = ?  " ;
		List achmentList = this.getHibernateTemplate().find(hql, cvId);
		if(achmentList != null){
			return achmentList.size();
		}
		return null;
	}

	
	/**
	 * 判断最后一份上传的简历 是否和 现在已经超过1年
	 */
	public boolean isUpdateResumeInformer(Integer cvId) {
		String hql = " from CvAttachment c where c.cvId = ?  order by c.createDt desc " ;
		List achmentList = this.getHibernateTemplate().find(hql, cvId);
		if(achmentList != null && achmentList.size() > 0 ){
			CvAttachment a = (CvAttachment)achmentList.get(0);
			Date  cDt = a.getCreateDt();
			if (cDt!=null){
				//cDt.setYear(cDt.getYear()+1);  //.把简历上传后，修改简历提供者的保护期从原来的1年，改为2年。
				Date adate=DateHelper.add(cDt, DateHelper.YEAR, 2);
				if(adate.compareTo(new Date()) == -1 ){
					//判断 0  相等  1 > -1 <
					return true;
				}
			}else{
				return true;  //如果没有创建时间 默认为 超过两年
			}
		}
		return false;
	}
	
	

}
